<template>
  <div class="data-table-container layout-padding">
    <el-card class="layout-padding-auto" shadow="hover">
      <el-form ref="queryFormRef" :model="state.tableData.param.params" inline>
        <el-form-item class="!mr-4" prop="phone">
          <el-input v-model="state.tableData.param.params.phone" class="max-w-180px" clearable
                    placeholder="请输入电话号码"
                    size="default">
          </el-input>
        </el-form-item>
        <el-form-item v-auth="'member.list'" class="!mr-4">
          <el-button size="default" type="primary" @click="getTableData">
            <el-icon>
              <ele-Search/>
            </el-icon>
            查询
          </el-button>
        </el-form-item>
        <el-form-item v-auth="'member.add'" class="!mr-4">
          <el-button size="default" type="success" @click="onOpenAddUser('add')">
            <el-icon>
              <ele-FolderAdd/>
            </el-icon>
            新增
          </el-button>
        </el-form-item>
        <el-form-item v-auth="'member.list'" class="!mr-4">
          <el-button size="default" type="primary" @click="onHandleResetFields">
            <el-icon>
              <ele-Refresh/>
            </el-icon>
            重置
          </el-button>
        </el-form-item>
        <el-form-item v-auth="'user.list'" class="!mr-4">
          <el-button
              size="default"
              type="warning"
              @click="handleExport"
          >
            <el-icon>
              <ele-Download/>
            </el-icon>
            导出
          </el-button>
        </el-form-item>
      </el-form>

      <!--显示字段-->
      <el-row :gutter="10" class="mb8">
        <right-toolbar :columns="state.tableData.columns" @queryTable="getTableData"></right-toolbar>
      </el-row>
      <el-table v-loading="state.tableData.loading" :data="state.tableData.data" style="width: 100%">
        <el-table-column v-if="state.tableData.columns[0].visible" label="ID" prop="id" show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[1].visible" label="会员昵称" prop="nickName"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[2].visible" label="手机号" prop="phone" show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[3].visible" label="微信" prop="wechat" show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[4].visible" label="openid" prop="openid" show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[5].visible" label="性别" prop="sex" show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="sys_user_sex" :value="scope.row.sex"/>
          </template>
        </el-table-column>
        <el-table-column v-if="state.tableData.columns[6].visible" label="积分" prop="totalPoints"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[7].visible" label="成长值" prop="totalDevelop"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[8].visible" label="会员等级" prop="memberGrade"
                         show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="member_grade" :value="scope.row.memberGrade"/>
          </template>
        </el-table-column>
        <el-table-column v-if="state.tableData.columns[9].visible" label="阳历生日" prop="solarBirthday"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[10].visible" label="阴历生日" prop="lunarBirthday"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[11].visible" label="顾问" prop="userName" show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[12].visible" label="消费次数" prop="totalConsumption"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[13].visible" label="充值金额" prop="rechargeAmount"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[14].visible" label="首次消费时间" prop="firstConsumptionTime"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[15].visible" label="上次消费时间" prop="lastConsumptionTime"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[16].visible" label="客单价" prop="customerPrice"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[17].visible" label="注册时间" prop="registrationTime"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[18].visible" label="注册来源" prop="registrationSource"
                         show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="member_source" :value="scope.row.registrationSource"/>
          </template>
        </el-table-column>
        <el-table-column v-if="state.tableData.columns[19].visible" label="推荐人" prop="referrerName"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[20].visible" label="转介绍" prop="introduction"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[21].visible" label="标签" prop="labelNames"
                         show-overflow-tooltip/>
        <el-table-column v-if="state.tableData.columns[22].visible" label="备注" prop="remark" show-overflow-tooltip/>
        <el-table-column label="操作" width="250">
          <template #default="scope">
            <el-button v-auth="'member.edit'" :disabled="scope.row.loginName === 'admin'" size="small" text
                       type="primary" @click="onOpenEditUser('edit', scope.row)">
              修改
            </el-button>
            <el-button v-auth="'member.del'" :disabled="scope.row.loginName === 'admin'" size="small" text
                       type="danger" @click="onRowDel(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination v-model:current-page="state.tableData.param.pageNum"
                     v-model:page-size="state.tableData.param.pageSize" :page-sizes="[10, 20, 30]" :pager-count="5"
                     :total="state.tableData.total" background class="mt15"
                     layout="total, sizes, prev, pager, next, jumper"
                     @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange">
      </el-pagination>
    </el-card>
    <MemberDialog ref="memberDialogRef" @refresh="getTableData()"/>
  </div>
</template>

<script lang="ts" name="systemUser" setup>
import {defineAsyncComponent, getCurrentInstance, onMounted, reactive, ref} from 'vue';
import {ElLoading, ElMessage, ElMessageBox} from 'element-plus';
import {memberInfoApi} from '/@/api/member/info';
import DictTag from "/@/components/DictTag/index.vue";
import {download} from "/@/utils/ruoyi";

// 引入组件
const MemberDialog = defineAsyncComponent(() => import('/@/views/member/info/dialog.vue'));


const {proxy} = getCurrentInstance();
const {member_source, sys_user_sex, member_grade} = proxy.useDict("member_source", "sys_user_sex", "member_grade");

// 定义变量内容
const memberDialogRef = ref();
const queryFormRef = ref();
const state = reactive<SysUserState>({
  tableData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      pageNum: 1,
      pageSize: 10,
      params: {
        phone: ''
      },
    },
    columns: [
      {key: 0, label: `ID`, visible: true},
      {key: 1, label: `会员昵称`, visible: true},
      {key: 2, label: `手机号`, visible: true},
      {key: 3, label: `微信`, visible: false},
      {key: 4, label: `openid`, visible: false},
      {key: 5, label: `性别`, visible: true},
      {key: 6, label: `积分`, visible: true},
      {key: 7, label: `成长值`, visible: false},
      {key: 8, label: `会员等级`, visible: true},
      {key: 9, label: `阳历生日`, visible: true},
      {key: 10, label: `阴历生日`, visible: false},
      {key: 11, label: `负责顾问`, visible: false},
      {key: 12, label: `消费次数`, visible: false},
      {key: 13, label: `充值金额`, visible: true},
      {key: 14, label: `首次消费时间`, visible: false},
      {key: 15, label: `上次消费时间`, visible: false},
      {key: 16, label: `客单价`, visible: false},
      {key: 17, label: `注册时间`, visible: false},
      {key: 18, label: `注册来源`, visible: true},
      {key: 19, label: `推荐人`, visible: true},
      {key: 20, label: `转介绍`, visible: false},
      {key: 21, label: `标签`, visible: false},
      {key: 22, label: `备注`, visible: false},
    ]
  },
});

// 初始化表格数据
const getTableData = () => {
  state.tableData.loading = true;
  memberInfoApi()
      .getTableData(state.tableData.param)
      .then((res) => {
        state.tableData.data = res.data.records;
        state.tableData.total = res.data.total;
        state.tableData.loading = false;
      })
      .catch(() => {
        state.tableData.loading = false;
      });
};
// 打开新增用户弹窗
const onOpenAddUser = (type: string) => {
  memberDialogRef.value.openDialog(type);
};
// 打开修改用户弹窗
const onOpenEditUser = (type: string, row: RowUserType) => {
  memberDialogRef.value.openDialog(type, row);
};
// 删除用户
const onRowDel = (row: RowUserType) => {
  ElMessageBox.confirm(`此操作将永久删除账户“${row.userName}”，是否继续?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    memberInfoApi()
        .delRecord(row)
        .then(() => {
          getTableData();
          ElMessage.success('删除成功');
        });
  }).catch(() => {
    ElMessage.info('已取消删除');
  });
};
// 分页改变
const onHandleSizeChange = (val: number) => {
  state.tableData.param.pageSize = val;
  getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
  state.tableData.param.pageNum = val;
  getTableData();
};
// 页面加载时
onMounted(() => {
  getTableData();
});
// 重置查询表单
const onHandleResetFields = () => {
  queryFormRef.value.resetFields();
  getTableData();
};


const handleExport = () => {
  let downloadLoadingInstance = ElLoading.service({
    text: "正在导出数据，请稍候",
    spinner: "el-icon-loading",
    background: "rgba(0, 0, 0, 0.7)",
  })
  memberInfoApi()
      .exportRecord(state.tableData.param.params)
      .then(res => {
        download(res, '会员信息表.xlsx');
        downloadLoadingInstance.close();
      })
}

</script>
